package com.panku.controller;

import com.alibaba.fastjson.JSONObject;
import com.panku.common.vo.Result;
import com.panku.entity.User;
import com.panku.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: jack
 * @Description:
 * @Date Create in 10:01 2021-01-21
 **/
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/add")
    public User add(@RequestBody User user) {
        userService.saveUser(user);
        return user;
    }

    @PostMapping("/get/{id}")
    public User getById(@PathVariable("id") Integer id) {
        User userById = userService.getUserById(id);
        return userById;
    }

    @PostMapping("/list/{page}/{size}")
    public Result list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page userPage = userService.userPages(page, size);
        JSONObject pageJson = new JSONObject();
        pageJson.put("total", userPage.getTotalElements());
        pageJson.put("pages", userPage.getTotalPages());
        pageJson.put("items", userPage.get().toArray());
        return Result.ok(pageJson);
    }


}
